package com.example.safeassistan.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.safeassistan.R;
import com.example.safeassistan.home.listener.OnGroupExpandedListener;
import com.example.safeassistan.home.model.HiddenDangerInvestigation;
import com.example.safeassistan.home.model.InvestigationModel;

import java.util.List;

public class HiddenDangerInvestigationParentAdapter extends BaseExpandableListAdapter {
    private List<InvestigationModel.ResultDTO> data;
    private Context context;
    private StringBuilder stringBuilder;
    public HiddenDangerInvestigationParentAdapter(Context context , List<InvestigationModel.ResultDTO> data) {
        this.context=context;
        this.data=data;
    }

    private OnGroupExpandedListener mOnGroupExpandedListener;

    public void setOnGroupExpandedListener(OnGroupExpandedListener onGroupExpandedListener) {
        mOnGroupExpandedListener = onGroupExpandedListener;
    }

    @Override
    public int getGroupCount() {
        return data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return  data.get(groupPosition).getChildren().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.get(groupPosition).getChildren();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition+childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup groupHolder;
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_hiddendanger_investigationp, parent,false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tvTitle);
            groupHolder.tvActivity=(TextView) convertView.findViewById(R.id.tvActivity);
            convertView.setTag(groupHolder);


            groupHolder.tv_group_name.setText(data.get(groupPosition).getItemName());
        }else {
            groupHolder=(ViewHolderGroup) convertView.getTag();
        }

        if (data.get(groupPosition).getIsselect()==null){
            data.get(groupPosition).setIsselect("true");
        }

        if(data.get(groupPosition).getIsselect().equals("true")){
            groupHolder.tvActivity.setText("活动");
            groupHolder.tvActivity.setTextColor(context.getResources().getColor(R.color.blue_3B5BEA));
        }else {
            groupHolder.tvActivity.setText("不涉及");
            groupHolder.tvActivity.setTextColor(context.getResources().getColor(R.color.gray6));
        }

        groupHolder.tvActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnGroupExpandedListener.onGroupExpanded(groupPosition);
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem holderItem=null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_hiddendanger_investigationc, parent,false);
            holderItem=new ViewHolderItem();
            convertView.setTag(holderItem);
        }else {
            holderItem=(ViewHolderItem)convertView.getTag();
        }
        holderItem.ivStatus=(ImageView) convertView.findViewById(R.id.ivStatus);
        holderItem.tvContent=(TextView)convertView.findViewById(R.id.tvContent);
        holderItem.tvId=(TextView)convertView.findViewById(R.id.tvId);


        if (data.get(groupPosition).getChildren().get(childPosition).getStatus()!=null){
            switch (data.get(groupPosition).getChildren().get(childPosition).getStatus()){
                case  0:
                    //通过
                    Glide.with(context).load(R.mipmap.icon_ok_green).into(holderItem.ivStatus);
                    holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.green));
                    break;
                case 1:
                    //不通过
                    Glide.with(context).load(R.mipmap.icon_no_red).into(holderItem.ivStatus);
                    holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.red));
                    break;
                case 2:
                    //存在隐患
                    Glide.with(context).load(R.mipmap.icon_danger_orange).into(holderItem.ivStatus);
                    holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.orange));
                    break;
                case 3:
                    //不涉及
                    Glide.with(context).load(R.mipmap.icon_norelative).into(holderItem.ivStatus);
                    holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.blue_3B5BEA));
                    break;
                case 4:
                    Glide.with(context).load(R.mipmap.icon_minus_gray).into(holderItem.ivStatus);
                    holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.gray3));
                    break;
            }
        }else {
            Glide.with(context).load(R.mipmap.icon_minus_gray).into(holderItem.ivStatus);
            holderItem.tvContent.setTextColor(context.getResources().getColor(R.color.gray3));
        }

        holderItem.tvId.setText((childPosition+1)+".");
        if (data.get(groupPosition).getChildren().get(childPosition).getItemDescList()!=null
                &&data.get(groupPosition).getChildren().get(childPosition).getItemDescList().size()>0){
            stringBuilder=new StringBuilder();
            for (int i=0;i<data.get(groupPosition).getChildren().get(childPosition).getItemDescList().size();i++){
                //遍历
                stringBuilder.append((childPosition+1)+"."+(i+1)+"、"+
                        data.get(groupPosition).getChildren().get(childPosition).getItemDescList().get(i)+"\n");
            }
            holderItem.tvContent.setText(stringBuilder);
        }else {
            holderItem.tvContent.setText(data.get(groupPosition).getChildren().get(childPosition).getItemDesc());
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_group_name;
        private TextView tvActivity;
    }

    private static class ViewHolderItem{
        private ImageView ivStatus;
        private TextView tvContent;
        private TextView tvId;
    }
}
